C#WPF实现回溯算法解决八皇后问题

运用C#WPF编程实现了经典的回溯算法解决“八皇后问题”

程序运行演示结果如下:




算法核心代码:

namespace nQueen
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        Queen[] queens;
        int[] queenPosition;
        Boolean[] isEnableColumn;
        Boolean[] isEnableLeft;
        Boolean[] isEnableRight;
        int queenNumber;
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindow()
        {
            queens = new Queen[8];
            initAl(8);
            InitializeComponent();
            //drawGrid(8);
        }
        /// <summary>
        /// 根据皇后数目绘制棋盘函数(暂不使用)
        /// </summary>
        /// <param name="input">皇后数目</param>
        public void drawQueen()
        {
            for (int i = 0; i < queenNumber; i++)
            {
                setQueen(i, queenPosition[i]);
            }
        }
        /// <summary>
        /// 初始化算法数据
        /// </summary>
        /// <param name="number"></param>
        public void initAl(int number)
        {
            queenPosition = new int[number];
            isEnableColumn = new Boolean[number];
            for (int i = 0; i < number; i++) { isEnableColumn[i] = true; }
            isEnableLeft = new Boolean[number * 2 - 1];
            for (int i = 0; i < number * 2 - 1; i++) { isEnableLeft[i] = true; }
            isEnableRight = new Boolean[number * 2 - 1];
            for (int i = 0; i < number * 2 - 1; i++) { isEnableRight[i] = true; }
            queenNumber = number;
        }

        /// <summary>
        /// 设置皇后的位置,如果此皇后已经存在则改变皇后的位置
        /// </summary>
        /// <param name="queenSeq">表示第几个皇后</param>
        /// <param name="row">表示皇后所在的行数</param>
        /// <param name="column">表示皇后所在的列数</param>
        public void setQueen(int row,int column)
        {
            if (queens[row] != null)
            {
                GridMain.Children.Remove(queens[row]);
                GridMain.UnregisterName("queen" + row.ToString());

            }
            else
            {
                queens[row] = new Queen();
            }
            queens[row].Name = "queen" + row.ToString();
            GridMain.Children.Add(queens[row]);
            GridMain.RegisterName("queen" + row.ToString(), queens[row]);
            Grid.SetRow(queens[row], row);
            Grid.SetColumn(queens[row], column);
        }
        //public void deleteQueen(int queenSeq);
        /// <summary>
        /// 开始运算按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            setQueen( 5, 2);
        }
        /// <summary>
        /// 初始化按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            putQueen(0);
            drawQueen();
            //string output="";
            //for (int i = 0; i < queenNumber; i++)
            //{ output += queenPosition[i].ToString();
            //output += "  ";
            //}
            //   MessageBox.Show(output);
        }
        /// <summary>
        /// 放置皇后 
        /// </summary>
        /// <param name="row">放置第N行的皇后</param>
        public bool putQueen(int row)
        {
            if (row == queenNumber) { return true; }
            for (int col = 0; col < queenNumber; col++)
            {
                
                //判断此列是否可用(列,左对角线,右对角线均为true)
                if (isEnableColumn[col] && isEnableLeft[row + col] && isEnableRight[row - col + queenNumber-1])
                {
                    queenPosition[row] = col;
                    isEnableColumn[col] = false;
                    isEnableLeft[row + col] = false;
                    isEnableRight[row - col + queenNumber-1] = false;
                    if(putQueen(row + 1))return true;                    
                    isEnableColumn[col] = true;
                    isEnableLeft[row + col] = true;
                    isEnableRight[row - col + queenNumber-1] = true;
                }
            }
            return false;
        }

    }
}


前台代码
<Grid Background="White" Name="GridMain" Width="560" Height="560" HorizontalAlignment="Center" Margin="10" VerticalAlignment="Bottom">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Border Background="{x:Null}" BorderBrush="Black" Grid.ColumnSpan="8" Grid.RowSpan="8" Width="560" Height="560" BorderThickness="4" Panel.ZIndex="1">
            
        </Border>
        
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle1" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="1" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle2" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="3" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle3" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="0"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle4" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="2" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle5" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="2" Grid.Column="1"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle6" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="2" Grid.Column="3"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle7" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="3" Grid.Column="0"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle8" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="3" Grid.Column="2"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle9" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="5" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle10" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="0" Grid.Column="7" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle11" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="4"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle12" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="1" Grid.Column="6" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle13" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="2" Grid.Column="5"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle14" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="2" Grid.Column="7"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle15" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="3" Grid.Column="4"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle16" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="3" Grid.Column="6"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle17" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="1" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle18" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="3" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle19" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="5"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle20" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="4" Grid.Column="7" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle21" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="5" Grid.Column="0"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle22" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="5" Grid.Column="2"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle23" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="5" Grid.Column="4"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle24" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="5" Grid.Column="6"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle25" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="1" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle26" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="3" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle27" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="5"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle28" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF" Grid.Row="6" Grid.Column="7" />
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle29" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="7" Grid.Column="0"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle30" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="7" Grid.Column="2"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle31" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="7" Grid.Column="4"/>
        <Rectangle Canvas.Left="0" Canvas.Top="0" Height="70" Name="rectangle32" Stroke="Black" Width="70" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="#FFB4AFAF"  Grid.Row="7" Grid.Column="6"/>
        <Grid Grid.ColumnSpan="8" Height="40" HorizontalAlignment="Center" Margin="0,-53,0,0" Name="grid1" VerticalAlignment="Top" Width="560" Panel.ZIndex="0" >
        <Button Content="开始求解" Height="30" HorizontalAlignment="Left" Name="startButton" VerticalAlignment="Top" Width="100" Panel.ZIndex="2" Foreground="Black" Margin="76,8,0,0" FontFamily="FZDaHei-B02S" FontWeight="Normal" FontStretch="Normal" Click="startButton_Click">
                
            </Button>
            <Button Content="test" FontFamily="FZDaHei-B02S" FontStretch="Normal" FontWeight="Normal" Foreground="Black" Height="30" Name="button2" Panel.ZIndex="2" Margin="333,8,127,0" Width="100" Click="button2_Click" />
           
        </Grid>
       
    </Grid>



posted on 2012-10-26 19:54  wizholy  阅读(298)  评论(0编辑  收藏  举报

导航